package net.dgg.dggcloud.service.jrcrm.service.mq;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Envelope;
import net.dgg.dggcloud.service.entity.user.User;
import net.dgg.dggcloud.service.service.user.UserService;
import net.dgg.framework.tac.mq.rabbitmq.queue.normal.DggAbstractNormalConsumer;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

/**
 * IBOSS用户消息处理
 *
 * @Title: UserConsumer
 * @Description:
 * @Auther: 汤宏
 * @Date: 2018/06/19 9:15
 */
public class UserConsumer extends DggAbstractNormalConsumer<Map> {

    @Resource
    private UserService userService;

    public UserConsumer() {
        this.queueName = "net.dgg.dggcloud.service.jrcrm.service.mq.UserConsumer";
        this.hasDeadQueue = false;
    }

    public void setExchangeName(String exchangeName) {
        this.exchangeName = exchangeName;
    }


    @Override
    protected void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, Map data) throws IOException {
   //     logger.info("IBOSS用户消息处理：{}" , data);
        try {
            if (null != data) {
                Map userMap = (Map) data.get("userEntity");
                User user = new User();
                user.setId(null != userMap.get("id") ? Long.parseLong(userMap.get("id").toString()) : null);
                user.setLoginName(null != userMap.get("loginName") ? userMap.get("loginName").toString() : "");
                user.setLoginPwd(null != userMap.get("loginPwd") ? userMap.get("loginPwd").toString() : "");
                user.setName(null != userMap.get("realName") ? userMap.get("realName").toString() : "");
                user.setOrgId(null != userMap.get("orgId") ? Long.parseLong(userMap.get("orgId").toString()) : null);
                user.setEmail(null != userMap.get("email") ? userMap.get("email").toString() : "");
                user.setPhone(null != userMap.get("phone") ? userMap.get("phone").toString() : "");
                user.setLocked(null != userMap.get("locked") ? Integer.parseInt(userMap.get("locked").toString()) : null);
                user.setSex(null != userMap.get("sex") ? ("1".equals(userMap.get("sex").toString()) ? "男" : "女") : "");
                user.setWorkAge(null != userMap.get("workAge") ? Integer.parseInt(userMap.get("workAge").toString()) : null);
                user.setWorkArea(null != userMap.get("workArea") ? userMap.get("workArea").toString() : "");
                user.setCallNum(null != userMap.get("seatNumber") ? userMap.get("seatNumber").toString() : "");
                Date entryDate = (null != userMap.get("entryDate") ? new Date(Long.parseLong(userMap.get("entryDate").toString())) : null);
                user.setEntryDate(entryDate);
                Date disDate = (null != userMap.get("dimissionTime") ? new Date(Long.parseLong(userMap.get("dimissionTime").toString())) : null);
                user.setDismissionTime(disDate);
                user.setaPhone(null != userMap.get("aphone") ? userMap.get("aphone").toString() : "");
                user.setxPhone(null != userMap.get("xphone") ? userMap.get("xphone").toString() : "");
                user.setVoipUsername(null != userMap.get("voipUserName") ? userMap.get("voipUserName").toString() : "");
                user.setVoipPwd(null != userMap.get("voipPwd") ? userMap.get("voipPwd").toString() : "");
                user.setShowTel(null != userMap.get("showTel") ? userMap.get("showTel").toString() : "");
                userService.saveBaseUser(user,userMap);
            }
        } catch (Exception e) {
            e.printStackTrace();
    //        logger.info("IBOSS用户消息处理异常：{}" ,data);
        }
    }
}
